package net.oov.identify.util;

public class CharExtUtil {
	/**
	 * return the string's reversal value 
	 * @param str original string
	 * @return reversal value 
	 */
	public static String reverse(String str){
		if(str.length()==1)return str;
		StringBuilder sb = new StringBuilder();
		for(int i=str.length()-1;i>=0;i--){
			sb.append(str.charAt(i));
		}
		return sb.toString();
	}
	
	public static char[] reverse(char[] cs){
		char[] temp = new char[cs.length];
		for(int i=0;i<cs.length;i++){
			temp[i] = cs[cs.length-1-i];
		}
		return temp;
	}
	
	/**
	 * return the string's reversal value 
	 * @param str original string
	 * @return reversal value 
	 */
	public static CharSequence reverse(CharSequence cs){
		if(cs.length()==1)return cs;
		StringBuilder sb = new StringBuilder();
		for(int i=cs.length()-1;i>=0;i--){
			sb.append(cs.charAt(i));
		}
		return sb.toString();
	}
	
	/**
	 * 非汉字、英文、数字的字符串
	 * @param s
	 * @return
	 */
	public static boolean isNoSence(String s){
		if(isEnglishLetter(s)||isArabicNumber(s)||isCJKCharacter(s)){
			return false;
		}
		return true;
	}
	
	/**
	 * @param s
	 * @return
	 */
	public static boolean isEnglishLetter(String s){
		for(char c:s.toCharArray()){
			if(!isEnglishLetter(c)){
				return false;
			}
		}
		return true;
	}
	
	/**
	 * @param s
	 * @return
	 */
	public static boolean isArabicNumber(String s){
		for(char c:s.toCharArray()){
			if(!isArabicNumber(c)){
				return false;
			}
		}
		return true;
	}
	
	/**
	 * @param s
	 * @return
	 */
	public static boolean isCJKCharacter(String s){
		for(char c:s.toCharArray()){
			if(!isCJKCharacter(c)){
				return false;
			}
		}
		return true;
	}
	
	public static boolean isNoSence(char input){
		if(isCJKCharacter(input)){
			return false;
		}else if(isEnglishLetter(input)){
			return false;
		}else if(isArabicNumber(input)){
			return false;
		}
		return true;
	}
	
	
	/**
	 * @param input
	 * @return
	 */
	public static boolean isEnglishLetter(char input){
		return (input >= 'a' && input <= 'z') 
				|| (input >= 'A' && input <= 'Z');
	}
	
	/**
	 * @param input
	 * @return
	 */
	public static boolean isArabicNumber(char input){
		return input >= '0' && input <= '9';
	}
	
	/**
	 * @param input
	 * @return
	 */
	public static boolean isCJKCharacter(char input){
		return Character.UnicodeBlock.of(input) 
					== Character.UnicodeBlock.CJK_UNIFIED_IDEOGRAPHS;
	}
	
	/**
	 * 进行字符规格化（全角转半角，大写转小写处理）
	 * @param input
	 * @return char
	 */
	public static char regularize(char input){
        if (input == 12288) {
            input = (char) 32;
            
        }else if (input > 65280 && input < 65375) {
            input = (char) (input - 65248);
            
        }else if (input >= 'A' && input <= 'Z') {
        	input += 32;
		}
        
        return input;
	}
	
}
